package com.hongxun.budget.ui.view.tk;

import android.app.Fragment;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.BCrossQuantitiesEntityDao;
import com.hongxun.budget.ui.activity.tk.ChoicePointActivity;
import com.hongxun.budget.ui.adapter.SpinnerAdapter;
import com.hongxun.budget.ui.fragment.tk.PointBasicFragment;
import com.hongxun.budget.ui.view.BaseView;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.budget.utils.TestingUtils;
import com.hongxun.greenentity.tk.BCrossQuantitiesEntity;
import com.hongxun.greenentity.tk.BDataDictionaryEntity;
import com.hongxun.greenentity.tk.DCrossCostEntity;
import com.hongxun.greenentity.tk.DPointEntity;

import java.util.List;

/**
 * Created by admin on 2018/8/27.
 */

public class CrossView extends BaseView implements View.OnClickListener {

    public static final int CHOICE_BEFORE_POINT = 0x8999;//选择上联点位
    public static final int CHOICE_END_POINT = 0x8998;//选择下联点位

    SpinnerAdapter<BDataDictionaryEntity> adapter;

    TextView before_point;
    TextView end_point;
    EditText span;
    EditText jiaodu;
    EditText c_mm;
    EditText width;
    EditText name;
    Spinner spinner;

    public CrossView(Fragment fragment) {
        super(fragment);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.view_cross;
    }

    @Override
    protected void initView() {
        super.initView();
        before_point = getView(R.id.before_point);
        before_point.setOnClickListener(this);
        end_point = getView(R.id.end_point);
        end_point.setOnClickListener(this);
        span = getView(R.id.span);
        jiaodu = getView(R.id.jiaodu);
        c_mm = getView(R.id.c_mm);
        name = getView(R.id.name);
        width = getView(R.id.width);
        spinner = getView(R.id.spinner);
        spinner.setAdapter(adapter = new SpinnerAdapter<BDataDictionaryEntity>(context, DBUtils.getBDataDictionaryEntities("cross_type"), spinner));
    }

    DCrossCostEntity d;

    @Override
    public boolean save(DPointEntity pointInfo) {
        d = new DCrossCostEntity();
        d.setGuid(pointInfo.getGuid());
        d.setPGuid(pointInfo.getPGuid());
        d.setPRGuid(pointInfo.getPRGuid());
        d.setCCName(name.getText().toString());
        d.setCType(adapter.getSelectedItem().getDKey());
        String mm = c_mm.getText().toString();
        String ww = width.getText().toString();
        String ss = span.getText().toString();
        if (!TestingUtils.isEmpty(context, before_point.getText().toString(), "上联杆号")) return false;
        d.setCCUpguid((String) before_point.getTag());
        if (!TestingUtils.isEmpty(context, end_point.getText().toString(), "下联杆号")) return false;
        d.setCCDownguid((String) end_point.getTag());
        if (TextUtils.isEmpty(mm) || Double.parseDouble(mm) == 0) {
            T.showLong(context, "导线切面积不能为0");
            return false;
        }
        if (TextUtils.isEmpty(ww) || Double.parseDouble(ww) == 0) {
            T.showLong(context, "跨越物宽度不能为0");
            return false;
        }
        d.setCCArea(Double.parseDouble(mm));
        d.setCCSpan(Double.parseDouble(ss));
        d.setCCWidth(Double.parseDouble(ww));
        d.setCCAutollatitude(pointInfo.getPPAutolatitude());
        d.setCCAutollongitude(pointInfo.getPPAutolongitude());
        d.setCCInputlatitude(pointInfo.getPPInputlatitude());
        d.setCCInputlongitude(pointInfo.getPPInputlongitude());
        if (TextUtils.isEmpty(jiaodu.getText().toString())) {
            T.showLong(context, "角度不能为空");
            return false;
        }
        d.setCCAngle(Double.parseDouble(jiaodu.getText().toString()));
        d.setCCUserid(pointInfo.getPPUserid());
        d.setCCCollecttime(pointInfo.getPPCollecttime());
        d.setCCDatastate(pointInfo.getPPDatastate());
        List<BCrossQuantitiesEntity> bCrossQuantitiesEntities = GreenDaoManager.getInstance().getmDaoSession().getBCrossQuantitiesEntityDao().queryBuilder()
                .where(BCrossQuantitiesEntityDao.Properties.CQAreaLower.le(mm)
                        , BCrossQuantitiesEntityDao.Properties.CQAreaHigh.ge(mm)
                        , BCrossQuantitiesEntityDao.Properties.CQWideLower.le(ww), BCrossQuantitiesEntityDao.Properties.CQWideHigh.ge(ww))
                .list();
        if (bCrossQuantitiesEntities.size() > 0) {
            d.setCCPrice(bCrossQuantitiesEntities.get(0).getCQPrice() * d.getCCSpan());
        }
        return true;
    }

    @Override
    public void insertDB() {
        GreenDaoManager.getInstance().getmDaoSession().getDCrossCostEntityDao().insertOrReplace(d);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, ChoicePointActivity.class);
        intent.putExtra("lat", ((PointBasicFragment) fragment).getLat());
        intent.putExtra("lgt", ((PointBasicFragment) fragment).getLgt());
        fragment.startActivityForResult(intent, (v.getId() == R.id.before_point) ? CHOICE_BEFORE_POINT : CHOICE_END_POINT);
    }

    @Override
    public void result(int requestCode, int resultCode, Intent data) {
        if (requestCode == CHOICE_BEFORE_POINT) {
            DPointEntity d = (DPointEntity) data.getSerializableExtra("point");
            String ppcode = "";
            String guid = "";
            if (d != null) {
                ppcode = d.getPPCode();
                guid = d.getGuid();
            }
            before_point.setText(ppcode);
            before_point.setTag(guid);
        } else if (requestCode == CHOICE_END_POINT) {
            DPointEntity d = (DPointEntity) data.getSerializableExtra("point");
            String ppcode = "";
            String guid = "";
            if (d != null) {
                ppcode = d.getPPCode();
                guid = d.getGuid();
            }
            end_point.setText(ppcode);
            end_point.setTag(guid);
        }
    }

    @Override
    public void updateInit(Object o) {
        DCrossCostEntity d = (DCrossCostEntity) o;
        int i = 0;
        for (BDataDictionaryEntity b : DBUtils.getBDataDictionaryEntities("cross_type")) {
            if (d.getCType().equals(b.getDKey())) {
                spinner.setSelection(i);
                break;
            }
            i++;
        }
        DPointEntity p = GreenDaoManager.getInstance().getmDaoSession().getDPointEntityDao().load(d.getCCUpguid());
        if (p != null) {
            before_point.setText(p.getPPCode());
            before_point.setTag(p.getGuid());
        }
        DPointEntity p2 = GreenDaoManager.getInstance().getmDaoSession().getDPointEntityDao().load(d.getCCDownguid());
        if (p2 != null) {
            end_point.setText(p2.getPPCode());
            end_point.setTag(p2.getGuid());
        }
        span.setText(d.getCCSpan() + "");
        width.setText(d.getCCWidth() + "");
        c_mm.setText(d.getCCArea() + "");
        jiaodu.setText(d.getCCAngle() + "");
        name.setText(d.getCCName());
    }

    @Override
    public void initDefault() {
        DCrossCostEntity d = GreenDaoManager.getInstance().getmDaoSession().getDCrossCostEntityDao().load(SPUtils.get(context, "UUID", "").toString());
        if (d != null) updateInit(d);
    }
}
